<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>写信</title>
		<link type="text/css" rel="stylesheet" href="../jquery-easyui-1.4.2/themes/default/easyui.css">
		<link type="text/css" rel="stylesheet" href="../jquery-easyui-1.4.2/themes/icon.css">
		<link rel="stylesheet" href="../ueditor/themes/default/_css/umeditor.css" />
		<script type="text/javascript" src="../js/jquery-2.1.1.min.js"></script>
		<script type="text/javascript" src="../jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../ueditor/umeditor.config.js" ></script>
		<script type="text/javascript" src="../ueditor/umeditor.min.js" ></script>
		<script type="text/javascript" src="../jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
		
		<style type="text/css">
			.td_text{
				text-align: right;
				padding-top: 8px;
			}
			.td_input{
				padding-left:15px ;
				padding-top: 10px;
			}
			.top_div_span{
				text-decoration: underline;
				margin-left: 5px;
				cursor: pointer;
			}
			#sessage_span{
				font-weight: bold;
				font-size: 14px;
				cursor:text;
				text-decoration: none;
			}
			#writeMessage_png{
				cursor:text;
			}
	</style>
	<script type="text/javascript">
		$(function(){
			//下拉框获取用户，填写到收件人文本框中
			$.post("../email/optionUser.do",function(data){
				$("#select_user").combobox({
					editable:false,//是否可编辑
					data:data,//把后台数据放入数组中
					valueField:'userId',//把数据放入value中
					textField:'userName',//把数据放入Text中
					//利用选中事件改变文本框及下拉框的值
					onSelect:function(record){
						var curValue=$("#select_user").combobox('getValue');
						var curText=$("#select_user").combobox('getText');
						$("#recipients").textbox('setText',curText);
						if(curValue>0){
							$("#select_user").combobox('setText','选择用户');
						}
					}
				});
			},"json");
			//存入草稿箱
			$("#saveDrafts").click(function(){
				var title = $("#titleId").combobox('getText');
				var content=UM.getEditor('editor').body.innerHTML;
				var addresserId = $("#userIdID").val();
				var addresser = $("#userNameId").val();
				var recipients = $("#recipients").combobox('getText');
				if(addresserId == "" && addresser == ""){
					$.messager.alert("错误信息","您还没有登录！","error");
					return;
				}
				if(recipients == ""){
					$.messager.alert("错误信息","收件人不能为空！","error");
					return;
				}
				if(title == ""){
					$.messager.alert("错误信息","标题不能为空！","error");
					return;
				}
				if(content == ""){
					$.messager.alert("错误信息","内容不能为空！","error");
					return;
				}
				$.post("<%=basePath%>email/draftAndOutBox.do",{
					draft:1,
					title:title,
					content:content,
					addresserId:addresserId,
					addresser:addresser,
					recipients:recipients
				},function(data){
					if(data == "success"){
						$.messager.alert("确认信息","数据保存至草稿箱!","info",function(){
							location.href="writeMessage.jsp";
						});
						 /* location.reload(); */
					}else if(data == "error"){
						$.messager.alert("错误信息","收件人姓名不存在！","error");
					}
				},"text");
			});
			
			//发送信息
			$("#sendMessage").click(function(){
				var title=$("#titleId").combobox('getText');
				var content=UM.getEditor('editor').body.innerHTML;
				var addresserId = $("#userIdID").val();
				var addresser = $("#userNameId").val();
				var recipients=$("#recipients").combobox('getText');
				if(addresserId == "" && addresser == ""){
					$.messager.alert("错误信息","您还没有登录！","error");
					return;
				}
				if(recipients == ""){
					$.messager.alert("错误信息","收件人不能为空！","error");
					return;
				}
				if(title == ""){
					$.messager.alert("错误信息","标题不能为空！","error");
					return;
				}
				if(content == ""){
					$.messager.alert("错误信息","内容不能为空！","error");
					return;
				}
				$.post("<%=basePath%>email/draftAndOutBox.do",{
					draft:0,
					title:title,
					content:content,
					addresserId:addresserId,
					addresser:addresser,
					recipients:recipients
				},function(data){
					if(data == "success"){
						$.messager.alert("确认信息","信息已发送！","info",function(){
							location.href="writeMessage.jsp";
						});
					}else if(data == "error"){
						$.messager.alert("错误信息","收件人姓名不存在！","error");
					}
				},"text");
			});
		});
	</script> 
</head>
	<body class="easyui-layout" style="margin: auto;">
		<div data-options="region:'north'" style="height:33px;padding:5px 0px 0px 5px">
			<span id="sessage_span">
				<span id="writeMessage_png" class="easyui-linkbutton" plain="true" iconCls="icon-writeMessage"></span>写信
			</span>
			<span class="top_div_span" id="sendMessage">发送</span> 
			<span class="top_div_span" id="saveDrafts">存草稿</span> 
			<span class="top_div_span">返回</span>
		</div>
		<div data-options="region:'center'">
		
			<input id="userIdID" type="hidden" value="${user.userId}">
			<input id="userNameId" type="hidden" value="${user.userName}">
			<table id="tableId" style="width: 95%;margin-left: 35px;margin-top: 25px;">
				<tr>
					<td class="td_text">收件人:</td>
					<td class="td_input"><input id="recipients" class="easyui-textbox" data-options="prompt:'收件人姓名'" style="width:70%">
						<select id="select_user" class="easyui-combobox" style="width:auto;"data-options="required:true,panelHeight:'auto'">   
	   						<option>选择用户</option>     
						</select>  
					</td>
				</tr>
				<tr>
					<td class="td_text">标题:</td>
					<td class="td_input"><input id="titleId" class="easyui-textbox"style="width:80%"></td>
				</tr>
				<tr>
					<td class="td_text">内容:</td>
					<td class="td_input">
						<textarea id="editor" style="width: 85%;height: 300px;"></textarea>
						<script type="text/javascript">
							//渲染编辑器
				    		var ue = UM.getEditor('editor');
						</script>
					</td>
				</tr>
				<tr>
					<td class="td_text">附件:</td>
					<td class="td_input">
						<form id="uploadFileForm" enctype="multipart/form-data">
							<input  class="easyui-filebox" id="fileUpload" name="file2" data-options="prompt:'点击右侧按钮选择文件',buttonText:'选择文件'" style="width:40%">(附件最多20M)<br />
							<label id="fileNameAndSize"></label>
						</form>
					</td>
				</tr>
			</table>
		</div>
	</body>
</html>